package com.example.chatroom.common.pojo;

import com.example.chatroom.common.errorcode.ErrorCode;
import com.example.chatroom.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {
    /**
     * 错误码
     */
    private Integer code;
    /**
     * 信息
     */
    private String msg;
    /**
     * 数据
     */
    private T data;

    /**
     * 构造方法
     */
    public CommonResult(Integer errorCode, String msg) {
        this.code = errorCode;
        this.msg = msg;
        this.data=null;
    }

    public CommonResult(T data) {
        this.data = data;
        this.code=200;
        this.msg = "成功";
    }

    public CommonResult(ErrorCode errorCode) {
        this.code=errorCode.getErrorCode();
        this.msg= errorCode.getMsg();
    }

    public CommonResult() {}

    /**
     * 失败
     */
    public static <T>  CommonResult<T> error(Integer errorCode,String msg){
        return new CommonResult(errorCode,msg);
    }

    public static <T>  CommonResult<T> error(ErrorCode errorCode){
        return new CommonResult<>(errorCode);
    }

    /**
     * 成功
     */
    public static <T>  CommonResult<T> success(T data){
        return new CommonResult<>(data);
    }
    public static <T>  CommonResult<T> success(){
        return new CommonResult<>(GlobalErrorCodeConstants.SUCCESS);
    }

}
